#
# Package.
#

package iSOCK::Controller::Datasheet::Commit;

#
# Base.
#

use Moose;
use namespace::autoclean;

BEGIN { extends 'iSOCK::Controller::Root' }

#
# Uses.
#

use SOCK::LOG;

=head1 NAME

iSOCK::Controller::Component - Component Controller for iSOCK

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

#
# Subs.
#

sub index :Path :Args(0) {
  my ( $self, $c ) = @_;

  my @logs = qw( isock_datasheet sock_lint_datasheet sock_atp_to_subversion );

  my $parameters = $c->request->parameters;

  if ( $c->request->method eq 'POST' && $parameters->{form_atp2svn_submit} ) {
    if ( my $session = $c->session ) {
      my $session_id   = $c->sessionid;
      my $session_path = $self->session_path ( $c );

      #
      # Variables.
      #

      my $ko;

      #
      # Log.
      #

      my $log = SOCK::LOG->new ({ file => "$session_path/isock_datasheet.log", fatality => 0 });

      my $info = $c->model ( 'App' )->info_string ( $c );

      #
      # Verbosity.
      #

      $log->say_n_right ( "Processing within $session_id session..." );

      #
      # Cleanup.
      #

      delete $session->{$_} for grep { ! $parameters->{$_} } qw( mif_file );

      unlink grep { not m/isock_datasheet/ } glob "$session_path/*.log";

      for ( $c->model ( 'Specification' ), $c->model ( 'Followup' ) ) {
        $_->delete_session_files ({ c => $c });
        $_->delete_session_keys  ({ c => $c });
      }

      #
      # Core.
      #

      my $model = $c->model ( 'Component' );

      $session->{do_atp2svn} = 1;

      if ( $parameters->{atp_number} ) {
        $model->atp2svn ({ c => $c, log => $log });
      }
      else {
        $ko = 1;
      }

      #
      # Verbosity.
      #

      $log->left_n_say ( "Done." );

      $log->close;

      $session->{ko} = $ko;

      #
      # Forward.
      #

      $c->response->redirect ( $c->uri_for ( '/logs' ) );
    }
  }
  else {
    $c->model ( 'Component' )->delete_session_keys ({ c => $c });
    
    $c->stash ( datasheets => $c->model ( 'App' )->datasheets_loh ( $c ) );
    
    $c->forward ( qw( iSOCK::Controller::Root render_view ) );
  }
}

=head1 AUTHOR

Xavier Caron

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

#
# End of iSOCK::Controller::Datasheet::Commit.
#

1;
